package desktop.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JPanel;
import java.awt.Color;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;
import java.awt.BorderLayout;
import javax.swing.SwingConstants;
import javax.swing.ImageIcon;
import javax.swing.filechooser.FileNameExtensionFilter;

import java.awt.Toolkit;
import javax.swing.JButton;
import javax.swing.border.EtchedBorder;

public class ViewPrincipal extends JFrame implements ActionListener {

	private static final long serialVersionUID = 1L;

	private JMenuItem mntmSair;
	private JMenuItem mntmAtivacao;
	private JButton btnLocalizar;
	private JButton btnConverter;
	private JLabel LBL_ARQUIVO_SELECIONADO;

	public ViewPrincipal() throws IOException {
		setIconImage(Toolkit.getDefaultToolkit().getImage(ViewPrincipal.class.getResource("/desktop/view/img/icon.png")));
		setTitle("Yousoft - Sistema para Convers\u00E3o de Arquivos");
		initComponents();
	}

	private static BufferedImage loadImage(URL url) throws IOException {
		return ImageIO.read(url);
	}	

	private void initComponents() throws IOException{

		JImagePanel imageBg = new JImagePanel(loadImage(ViewPrincipal.class.getResource("/desktop/view/img/bg.png")));

		JPanel panel = new JPanel();
		panel.setBackground(new Color(6, 129, 185));

		JLabel lblDesenvolvidoPorLeafsoftware = new JLabel("Desenvolvido por Yousoft \u00A9 2013 - Todos os direitos reservados");
		lblDesenvolvidoPorLeafsoftware.setFont(new Font("Tahoma", Font.PLAIN, 11));
		lblDesenvolvidoPorLeafsoftware.setForeground(Color.WHITE);
		panel.add(lblDesenvolvidoPorLeafsoftware);
		
		JPanel panel_convert = new JPanel();
		panel_convert.setBackground(Color.WHITE);

		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addComponent(panel_convert, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 684, Short.MAX_VALUE)
						.addComponent(imageBg, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 684, Short.MAX_VALUE)
						.addComponent(panel, GroupLayout.DEFAULT_SIZE, 684, Short.MAX_VALUE))
					.addGap(0))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addComponent(panel_convert, GroupLayout.PREFERRED_SIZE, 131, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(imageBg, GroupLayout.DEFAULT_SIZE, 286, Short.MAX_VALUE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE))
		);
		
		JLabel lblNewLabel_1 = new JLabel("  .: Conversor Sob Medida de arquivos :.");
		lblNewLabel_1.setFont(new Font("Tahoma", Font.BOLD, 16));
		lblNewLabel_1.setIcon(new ImageIcon(ViewPrincipal.class.getResource("/desktop/view/img/icon_convert.png")));
		
		JLabel lblArqui = new JLabel("Arquivo Localizado:");
		lblArqui.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 11));
		
		LBL_ARQUIVO_SELECIONADO = new JLabel("LBL ARQUIVO SELECIONADO");
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		GroupLayout gl_panel_convert = new GroupLayout(panel_convert);
		gl_panel_convert.setHorizontalGroup(
			gl_panel_convert.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_convert.createSequentialGroup()
					.addGroup(gl_panel_convert.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel_convert.createSequentialGroup()
							.addContainerGap()
							.addComponent(panel_1, GroupLayout.DEFAULT_SIZE, 664, Short.MAX_VALUE))
						.addGroup(gl_panel_convert.createParallelGroup(Alignment.TRAILING, false)
							.addGroup(Alignment.LEADING, gl_panel_convert.createSequentialGroup()
								.addContainerGap()
								.addComponent(lblNewLabel_1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
							.addGroup(Alignment.LEADING, gl_panel_convert.createSequentialGroup()
								.addGap(39)
								.addComponent(lblArqui, GroupLayout.PREFERRED_SIZE, 111, GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(LBL_ARQUIVO_SELECIONADO, GroupLayout.PREFERRED_SIZE, 354, GroupLayout.PREFERRED_SIZE))))
					.addContainerGap())
		);
		gl_panel_convert.setVerticalGroup(
			gl_panel_convert.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_convert.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblNewLabel_1, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_panel_convert.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblArqui)
						.addComponent(LBL_ARQUIVO_SELECIONADO))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 53, Short.MAX_VALUE)
					.addContainerGap())
		);
		
		btnLocalizar = new JButton("  Localizar");
		btnLocalizar.addActionListener(this);
		btnLocalizar.setIcon(new ImageIcon(ViewPrincipal.class.getResource("/desktop/view/img/find.png")));
		
		btnConverter = new JButton("  Converter");
		btnConverter.setIcon(new ImageIcon(ViewPrincipal.class.getResource("/desktop/view/img/1368581546_check.png")));
		GroupLayout gl_panel_1 = new GroupLayout(panel_1);
		gl_panel_1.setHorizontalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addContainerGap()
					.addComponent(btnLocalizar)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(btnConverter, GroupLayout.PREFERRED_SIZE, 115, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(481, Short.MAX_VALUE))
		);
		gl_panel_1.setVerticalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, gl_panel_1.createSequentialGroup()
					.addContainerGap(12, Short.MAX_VALUE)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnLocalizar, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnConverter, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE))
					.addContainerGap())
		);
		panel_1.setLayout(gl_panel_1);
		panel_convert.setLayout(gl_panel_convert);
		imageBg.setLayout(new BorderLayout(0, 0));

		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setIcon(new ImageIcon(ViewPrincipal.class.getResource("/desktop/view/img/logo_yousoft.png")));
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		imageBg.add(lblNewLabel);
		getContentPane().setLayout(groupLayout);

		java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
		setBounds((screenSize.width-700)/2, (screenSize.height-550)/2, 700, 513);

		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
				
						JMenu mnSistema = new JMenu("Sistema");
						menuBar.add(mnSistema);
						
								mntmAtivacao = new JMenuItem("   Ativa\u00E7\u00E3o do Sistema       ");
								mntmAtivacao.addActionListener(this);
								mnSistema.add(mntmAtivacao);
								
										JSeparator separator = new JSeparator();
										mnSistema.add(separator);
										
												mntmSair = new JMenuItem("   Sair");
												mnSistema.add(mntmSair);
												
														JSeparator separator_1 = new JSeparator();
														mnSistema.add(separator_1);

		setExtendedState(MAXIMIZED_BOTH);

	}

	@Override
	public void actionPerformed(ActionEvent e) {

		JComponent componente = (JComponent) e.getSource();
		if(componente.equals(mntmAtivacao)){
			DialogAtivacao d = new DialogAtivacao();
			d.setVisible(true);
		}else
			if(componente.equals(btnLocalizar)){
				JFileChooser chooser = new JFileChooser();
				FileNameExtensionFilter filter = new FileNameExtensionFilter(
						"Arquivos .xml", "xml");
				chooser.setFileFilter(filter);
				int returnVal = chooser.showOpenDialog(this);
				if(returnVal == JFileChooser.APPROVE_OPTION) {
					System.out.println("You chose to open this file: " +
							chooser.getSelectedFile().getName());
				}
			}


	}
}
